package nl.weeaboo.sh.danmaku;

public class SpellBook extends Spell {

	private Spell children[];
	private int index;
	private int loopsCompleted;
	private int totalLoops;
	
	public SpellBook(int loops) {
		this(new SpellBook[0], loops);
	}
	public SpellBook(Spell ch[], int loops) {
		children = SpellUtil.copyOfRange(ch, 0);
		totalLoops = loops;
	}
	protected SpellBook(SpellBook t) {
		super(t);
		children = SpellUtil.copyOfRange(t.children, 0);
		index = t.index;
	}
	
	//Functions
	public SpellBook clone() {
		return new SpellBook(this);
	}
	public void reset() {
		super.reset();
		
		for (Spell s : children) {
			s.reset();
		}
		index = 0;
		loopsCompleted = 0;
	}
		
	@Override
	public void step(Universe u, SpellBook sb, ShotModel s) {
		while (!isFinished()) {
			if (!children[index].isFinished()) {
				children[index].step(u, this, s);
			}
			if (children[index].isFinished()) {
				index++;
			} else {
				break;
			}
		}
		if (index >= children.length) {
			loopsCompleted++;
			if (totalLoops < 0 || loopsCompleted < totalLoops) {
				index = 0;
			}
		}
	}
	
	//Getters
	public boolean isFinished() { return finished = index >= children.length; }
	
	//Setters
	
}
